package com.example.pingze.service;

import com.example.pingze.entity.Sentence;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.SentenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SentenceService {
    @Autowired
    private SentenceRepository repository;

    //通过诗人名获取名句
    public List<Sentence> getSentences(String sentenceFrom) {
        return repository.findSentenceBySentenceFromContains(sentenceFrom);
    }

    public Page getSentenceList(Pageable pageable) {
        return repository.findAll(pageable);
    }

    public Page findSentenceList(Sentence sentence, Pageable pageable) {
        Specification<Sentence> specification=new Specification<Sentence>() {
            @Override
            public Predicate toPredicate(Root<Sentence> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(sentence.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),sentence.getId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }

    public Sentence edit(String id, String sentence, String sentenceFrom) {
        Sentence edit = repository.findSentenceById(id);
        if(!edit.getSentence().equals(sentence)){
            edit.setSentence(sentence);
        }
        if(!edit.getSentenceFrom().equals(sentenceFrom)){
            edit.setSentenceFrom(sentenceFrom);
        }
        repository.save(edit);
        return edit;
    }

    public Sentence add(String sentence, String sentenceFrom) {
        if(StringUtils.isEmpty(sentence)){
            throw new PingZeException("名句不能为空");
        }
        if(StringUtils.isEmpty(sentenceFrom)){
            throw new PingZeException("名句来源不能设置为空");
        }
        Sentence add=new Sentence();
        add.setId(UUID.randomUUID().toString());
        add.setSentence(sentence);
        add.setSentenceFrom(sentenceFrom);
        repository.save(add);
        return add;
    }

    public Sentence findSentenceById(String id) {
        return repository.findSentenceById(id);
    }

    public void delete(Sentence sentence) {
        repository.delete(sentence);
    }

    public List<Sentence> findSentencesBySentence(String keyword) {
        return repository.findSentencesBySentenceContains(keyword);
    }

    public Sentence findSentenceBySentence(String answer) {
        return repository.findSentenceBySentenceContains(answer);
    }

    //多条件查询（获取名句信息）
//    public List<Sentence> getSentence(String writer, String title) {
//
//    }

}
